package org.jsola.hr.web;

import org.apache.commons.lang3.StringUtils;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.OfferInfoAddDTO;
import org.jsola.hr.dto.OfferInfoUpdateDTO;
import org.jsola.hr.query.OfferInfoQuery;
import org.jsola.hr.service.IOfferInfoService;
import org.jsola.hr.vo.OfferInfoListVO;
import org.jsola.hr.vo.OfferInfoVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;


/**
 * @author zhr
 */
@RestController("hrOfferInfoController")
@RequestMapping(HrConstants.API_PATH + "/v1/offerInfos")
public class OfferInfoController implements IOfferInfoControllerAPI {

    @Autowired
    private IOfferInfoService offerInfoService;

    @Override
    @PostMapping
    public Result<OfferInfoVO> save(@Valid @RequestBody OfferInfoAddDTO offerInfoAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工offer信息权限校验

        return Result.success(
                offerInfoService.save(offerInfoAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody OfferInfoUpdateDTO offerInfoUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工offer信息权限校验

        int count = offerInfoService.update(offerInfoUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{offerInfoId}")
    public Result<Integer> delete(@PathVariable String offerInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工offer信息权限校验

        int count = offerInfoService.deleteByIds(tokenUser, offerInfoId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] offerInfoIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工offer信息权限校验

        int count = offerInfoService.deleteByIds(tokenUser, offerInfoIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{offerInfoId}")
    public Result<OfferInfoVO> findById(@PathVariable String offerInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工offer信息权限校验

        return Result.success(
            offerInfoService.selectById(offerInfoId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<OfferInfoListVO>> listAll(@Valid OfferInfoQuery offerInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工offer信息权限校验

        return Result.success(
            offerInfoService.select(offerInfoQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<OfferInfoListVO>> page(@Valid OfferInfoQuery offerInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            offerInfoService.selectPage(offerInfoQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping(value = "/saveAndSend")
    public Result<OfferInfoVO> saveAndSend(@Valid @RequestBody OfferInfoAddDTO offerInfoAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工offer信息权限校验

        return Optional.ofNullable(offerInfoService.saveAndSend(offerInfoAddDTO, tokenUser))
                .map(vo -> Result.success(vo))
                .orElseGet(() -> Result.fail("offer信息保存异常，操作失败"));
    }

    @Override
    @GetMapping(value = "/getOfferInfoById")
    public Result<OfferInfoVO> getOfferInfoById(String offerInfoId, String siteId) {
        if(StringUtils.isBlank(offerInfoId)){
            return Result.fail("员工offer信息id不可为空");
        }
        return Optional.ofNullable(offerInfoService.getOfferInfoById(offerInfoId, siteId))
                .map(vo -> Result.success(vo))
                .orElseGet(() -> Result.fail("offer信息不存在或已过期"));
    }
}

